<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;

class ResetController extends Controller
{
    //
    public function forget()
    {
        return view('admin.reset.email');
    }

    public function send(Request $request)
    {
       //发送邮件
        // Mail::raw('这里是邮件的内容,',function($message){
        //     $message -> to('271648298@qq.com');
        //     $message -> subject('这是邮件的主题');
        // });
        // //发送带模板的文件
        // Mail::send('email.forget',[],function($message){
        //     $message -> to('271648298@qq.com');
        //     $message -> subject('这是邮件的主题');
        // });
        $email = $request -> input('email');

        $arr = explode('@',$email);

        // dd($arr);
        $tiaoEmail=  'http://mail.'.$arr[1];


        $res = DB::table('admins') -> where('email',$email) -> first();
        if(!$res)
        {
            return back() -> with(['info' => '邮箱不存在']);
        }
        //function($message) use $email{}
        //闭包函数  用外部数据先use
        Mail::send('email.forget',['res' => $res],function($message) use($email){
            $message -> to($email);
            $message -> subject('这是邮件的主题');
        });

        return view('admin.reset.tiao',['tiaoEmail' => $tiaoEmail]);
    }

    public function find($token){
        // echo $token;
        $res = DB::table('admins') -> where('token',$token) -> first();
        if(!$res)
        {

            return redirect('/admin/reset/info') -> with(['info' => '不是一个合法的来源,请走开']);
        }
        return view('/admin.reset.newpass',['res' => $res]);
    }

    public function info()
    {
        return view('admin.reset.info');
    }

    public function uppass(Request $request)
    {
        $data = $request -> except('_token');

        $password = Crypt::encryptString($data['password']);

        $res = DB::table('admins') -> where('email',$data['email']) -> update(['password' => $password]);
        if($res)
        {
            return redirect('/admin/login') -> with(['info' => '修改成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }

    }
}
